<?php
/**
 * Created by PhpStorm.
 * User: junyv
 * Date: 2017/3/31
 * Time: 11:04
 */

namespace app\home\controller;


class Question extends Base
{
    private $cate;
    public function _initialize()
    {
        parent::_initialize();
        $cate = cache('askCate');
        $this->cate = $cate;
        $this->assign('cate',$cate);
    }
    public function index(){
        $cate_id = input('param.cate_id/d',0);
        $solve   = input('param.solve/d',0);
        $keyword = input('get.keyword');
        $pid     = 0;
        $where['status'] = 1;
        $where['city']   = $this->cityInfo['id'];
        if($solve == 1){
            $where['option'] = 1;
        }elseif($solve == 2){
            $where['option'] = 0;
        }
        if($cate_id){
            if(array_key_exists($cate_id,$this->cate['cate'])){
                $pid = $this->cate['cate'][$cate_id]['pid'];
            }
            if(isset($this->cate['tree'][$cate_id]) && isset($this->cate['tree'][$cate_id]['_child'])){
                $cateid = array_keys($this->cate['tree'][$cate_id]['_child']);
                $where['cate_id'] = ['in',$cateid];
            }else{
                $where['cate_id'] = $cate_id;
            }
        }
        if($pid==0){
            $pid = $cate_id;
        }
        $keyword && $where['title'] = ['like','%'.$keyword.'%'];
        $lists   = model('ask')->where($where)->field('id,title,option,cate_id,answer,create_time')->order('id desc')->paginate(15);
        $this->assign('lists',$lists);
        $this->assign('pages',$lists->render());
        $this->assign('cate_id',$cate_id);
        $this->assign('pid',$pid);
        $this->assign('solve',$solve);
        return $this->fetch();
    }
    public function detail(){
        $id = input('param.id/d',0);
        $info = '';$house_info = '';$house_title='';
        if($id){
            model('ask')->where(['id'=>$id,'status'=>1])->setInc('hits');
            $info = model('ask')->where(['id'=>$id,'status'=>1])->find();
            if(!$info){
                return $this->fetch('public/404');
            }
            if($info['house_id']){
                $house_info = model('house')->where(['id'=>$info['house_id'],'status'=>1])->field('id,title')->find();
            }
            if($house_info && isset($house_info['title'])){
                $house_title = $house_info['title'];
            }else{
                $house_title = '';
            }
            $moreAnswer = model('answer')->where(['ask_id'=>$id,'status'=>1])->order('id desc')->paginate(10);
            $this->assign('moreAnswer',$moreAnswer);
            $this->assign('pages',$moreAnswer->render());
        }else{
            return $this->fetch('public/404');
        }
        $info['seo_keys'] = $house_title.','.$info['title'];
        $info['title']    = $house_title.$info['title'];
        $this->setSeo($info);
        $this->assign('info',$info);
        $this->assign('house_info',$house_info);
        return $this->fetch();
    }
    public function add(){
        return  $this->fetch();
    }
}